using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameModeManager : MonoBehaviour
{
	public static GameModeManager instance;

	private static GameModeConfig _gameModeConfig;

	private List<GameModeConfig.ModeInfo> _modes;

	private int _gridMaxPerLine = 4;

	private bool _isWide;

	private bool _needSoonButton;

	public static GameModeConfig GameModeConfig
	{
		get
		{
			if (_gameModeConfig == null)
			{
				_gameModeConfig = Resources.Load<GameModeConfig>("GameModeConfig");
			}
			return _gameModeConfig;
		}
	}

	public List<GameModeConfig.ModeInfo> ModesConfig
	{
		get
		{
			if (_modes == null)
			{
				GameModeConfig gameModeConfig = GameModeConfig;
				_modes = gameModeConfig.ModesConfig;
			}
			return _modes;
		}
	}

	public int GridMaxPerLine
	{
		get
		{
			return _gridMaxPerLine;
		}
		set
		{
			_gridMaxPerLine = value;
		}
	}

	public bool IsWide
	{
		get
		{
			return _isWide;
		}
		set
		{
			_isWide = value;
		}
	}

	public bool NeedSoonButton
	{
		get
		{
			return _needSoonButton;
		}
		set
		{
			_needSoonButton = value;
		}
	}

	private void Awake()
	{
		instance = this;
		SetGridSetting();
		StartCoroutine(InitModeName());
		StartCoroutine(InitButtonsInSelectStandartUI());
		StartCoroutine(InitButtonsInSelectTransportUI());
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void SetGridSetting()
	{
		int num = Mathf.RoundToInt(177.77779f);
		int num2 = Mathf.RoundToInt(160f);
		int num3 = Mathf.RoundToInt((float)Screen.width / (float)Screen.height * 100f);
		if (num3 == num)
		{
			GridMaxPerLine = 5;
			IsWide = true;
		}
		else if (num3 == num2)
		{
			IsWide = true;
		}
		else
		{
			GridMaxPerLine = 4;
			IsWide = false;
		}
		NeedSoonButton = ModesConfig.Count % GridMaxPerLine != 0;
	}

	private IEnumerator InitModeName()
	{
		List<GameModeConfig.ModeInfo> tmpModesList = new List<GameModeConfig.ModeInfo>();
		for (int i = 0; i < ModesConfig.Count; i++)
		{
			GameModeConfig.ModeInfo tmpModeInfo = ModesConfig[i];
			tmpModeInfo.GetTranslateName();
			tmpModeInfo.GetAnimation();
			tmpModeInfo.GetTexture();
			tmpModeInfo.GetAnimationPreview();
			tmpModesList.Add(tmpModeInfo);
			yield return null;
		}
		_modes = new List<GameModeConfig.ModeInfo>(tmpModesList);
		yield return null;
	}

	private IEnumerator InitButtonsInSelectStandartUI()
	{
		SelectStandartUI selectStandart = GetComponentInChildren<SelectStandartUI>(true);
		for (int i = 0; i < 8; i++)
		{
			SelectableButtonShop tmpButton = Object.Instantiate(selectStandart.ExampleShopButton);
			tmpButton.gameObject.SetActive(true);
			tmpButton.transform.SetParent(selectStandart.SelectableButtonShopController.grid.transform);
			tmpButton.transform.localScale = Vector3.one;
			tmpButton.TransportInfoPanel = selectStandart.TransportInfoPanel;
			selectStandart.SelectableButtonShopController.AddNewButton(tmpButton);
			yield return null;
		}
		if (IsWide)
		{
			selectStandart.SelectableButtonShopController.grid.cellWidth = 240f;
		}
		selectStandart.SelectableButtonShopController.grid.repositionNow = true;
		yield return null;
	}

	private IEnumerator InitButtonsInSelectTransportUI()
	{
		SelectTransportUI selectTransport = GetComponentInChildren<SelectTransportUI>(true);
		for (int i = 0; i < 4; i++)
		{
			SelectableButtonShop tmpButton = Object.Instantiate(selectTransport.ExampleShopButton);
			tmpButton.gameObject.SetActive(true);
			tmpButton.transform.SetParent(selectTransport.SelectableButtonShopController.grid.transform);
			tmpButton.transform.localScale = Vector3.one;
			tmpButton.TransportInfoPanel = selectTransport.TransportInfoPanel;
			tmpButton.SelectableButtonController = selectTransport.SelectableButtonController;
			selectTransport.SelectableButtonShopController.AddNewButton(tmpButton);
			yield return null;
		}
		selectTransport.SelectableButtonShopController.grid.repositionNow = true;
		yield return null;
	}
}
